package com.cn.xiaonuo.tcrfinance.sysmgmt.controller;

import com.cn.xiaonuo.common.constant.SystemConstant;
import com.cn.xiaonuo.common.entity.SysLog;
import com.cn.xiaonuo.common.utils.ApiJsonModel;
import com.cn.xiaonuo.common.validated.util.ValidatorUtils;
import com.cn.xiaonuo.core.annotion.BusinessLog;
import com.cn.xiaonuo.core.enums.LogAnnotionOpTypeEnum;
import com.cn.xiaonuo.core.pojo.login.SysLoginUser;
import com.cn.xiaonuo.core.pojo.response.ResponseData;
import com.cn.xiaonuo.core.pojo.response.SuccessResponseData;
import com.cn.xiaonuo.tcrfinance.tcrorder.service.PvOrderService;
import com.cn.xiaonuo.tcrfinance.sysmgmt.entity.SysUserHpcEntity;
import com.cn.xiaonuo.tcrfinance.sysmgmt.param.PasswordHpcParam;
import com.cn.xiaonuo.tcrfinance.sysmgmt.param.SysUserHpcParam;
import com.cn.xiaonuo.tcrfinance.sysmgmt.service.SysUserHpcService;
import com.cn.xiaonuo.tcrfinance.sysmgmt.service.SysUserRoleHpcService;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 系统用户
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
public class SysUserHpcController  {
	@Autowired
	private SysUserHpcService sysUserService;
	@Autowired
	private SysUserRoleHpcService sysUserRoleService;
	@Autowired
	private PvOrderService pvOrderService;

	/**
	 * 所有用户列表
	 */
	@PostMapping("/api/sys/user/list")
	@ApiOperation(value = "管理员列表", notes = "管理员列表")
	public ResponseData list(SysUserHpcParam sysUserHpcParam){
		//只有超级管理员，才能查看所有管理员列表
		SysLoginUser loginUser=(SysLoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		if(loginUser.getId()!= SystemConstant.SUPER_ADMIN){
			sysUserHpcParam.setCreateUserId(loginUser.getId());
		}
		return new SuccessResponseData(sysUserService.queryPage(sysUserHpcParam));
	}

	/**
	 * 修改登录用户密码
	 */
	@SysLog("修改密码")
	@BusinessLog(title = "修改密码", opType = LogAnnotionOpTypeEnum.DETAIL)
	@ApiOperation(value = "修改密码", notes = "修改密码")
	@PostMapping("/api/sys/user/password")
	public ResponseData password(@RequestBody PasswordHpcParam passwordParam){
		SuccessResponseData responseData=new SuccessResponseData();
		if(StringUtils.isBlank(passwordParam.getPassword())){
			responseData.setSuccess(false);
			responseData.setMessage("新密码不为能空");
			return  responseData;
		}
		SysLoginUser loginUser=(SysLoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		SysUserHpcEntity user=sysUserService.getById(loginUser.getId());
		//sha256加密
		String password = new Sha256Hash(passwordParam.getPassword(), user.getSalt()).toHex();
		//sha256加密
		String newPassword = new Sha256Hash(passwordParam.getNewPassword(), user.getSalt()).toHex();

		//更新密码
		boolean flag = sysUserService.updatePassword(loginUser.getId(), password, newPassword);
		if(!flag){
			responseData.setSuccess(false);
			responseData.setMessage("原密码不正确");
			return  responseData;
		}

		return responseData;
	}

	/**
	 * 用户信息
	 */
	@PostMapping("/api/sys/user/info")
	@ApiOperation(value = "用户信息", notes = "用户信息")
//	@RequiresPermissions("sys:user:info")
	public ResponseData info(@RequestBody SysUserHpcParam sysUserParam){
		Long userId=sysUserParam.getUserId();
		SysUserHpcEntity user = sysUserService.getById(userId);

		//获取用户所属的角色列表
		List<Long> roleIdList = sysUserRoleService.queryRoleIdList(userId);
		user.setRoleIdList(roleIdList);
		return new SuccessResponseData(user);
	}

	/**
	 * 保存用户
	 */
	@SysLog("新增用户")
	@BusinessLog(title = "新增用户", opType = LogAnnotionOpTypeEnum.DETAIL)
	@ApiOperation(value = "新增用户", notes = "新增用户")
	@PostMapping("/api/sys/user/save")
	public ResponseData save(@RequestBody SysUserHpcParam sysUserParam){
		ValidatorUtils.validateEntity(sysUserParam);
		SysLoginUser loginUser=(SysLoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		sysUserParam.setCreateUserId(loginUser.getId());
		sysUserService.saveUser(sysUserParam);
		return new SuccessResponseData();
	}

	/**
	 * 修改用户
	 */
	@SysLog("修改用户")
	@BusinessLog(title = "修改用户", opType = LogAnnotionOpTypeEnum.DETAIL)
	@ApiOperation(value = "修改用户", notes = "修改用户")
	@PostMapping("/api/sys/user/update")
	public ResponseData update(@RequestBody SysUserHpcParam sysUserParam){
		ValidatorUtils.validateEntity(sysUserParam);
		SysLoginUser loginUser=(SysLoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		sysUserParam.setCreateUserId(loginUser.getId());
		sysUserService.update(sysUserParam);

		return new SuccessResponseData();
	}

	/**
	 * 删除用户
	 */
	@SysLog("删除用户")
	@BusinessLog(title = "删除用户", opType = LogAnnotionOpTypeEnum.DELETE)
	@ApiOperation(value = "删除用户", notes = "删除用户")
	@PostMapping("/api/sys/user/delete")
	public ResponseData delete(@RequestBody  SysUserHpcParam sysUserHpcParam){
		SuccessResponseData responseData=new SuccessResponseData();
		Long[] userIds=sysUserHpcParam.getUserIds();
		if(ArrayUtils.contains(userIds, 1L)){
			responseData.setSuccess(false);
			responseData.setMessage("系统管理员不能删除");
			return  responseData;
		}
		SysLoginUser loginUser=(SysLoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		if(ArrayUtils.contains(userIds, loginUser.getId())){
			responseData.setSuccess(false);
			responseData.setMessage("当前用户不能删除");
			return  responseData;
		}

		sysUserService.deleteBatch(userIds);

		return new SuccessResponseData();
	}

	@ApiOperation(value = "摘单商品列表", notes = "摘单商品列表")
	@ApiJsonModel({
			@ApiModelProperty(name = "pickProductId", value = "商品id",example = "商品id（true）",required = true),
			@ApiModelProperty(name = "pageSize", value = "每页多少数量",example = "每页多少数量（true）",required = true),
			@ApiModelProperty(name = "pageNo", value = "页码",example = "页码（true）",required = true)
	})
	@RequestMapping(value = "/api/common/product/queryHtProductList", method = RequestMethod.POST)
	public ResponseData queryHtProductList(@RequestBody Map<String,Object> params){
		return new SuccessResponseData(pvOrderService.queryHtProductList(params));
	}

}
